package phan.connection;

import java.io.*;
import javax.jms.Session;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.fileupload.*;
import org.apache.tomcat.util.http.fileupload.FileUpload;
import org.apache.commons.fileupload.DiskFileUpload;
import java.util.List;
import org.apache.commons.fileupload.disk.*;
import org.apache.commons.fileupload.servlet.*;
import java.util.*;
import java.io.*;
import java.io.InputStream;
import java.sql.*;

/**
 * Servlet implementation class for Servlet: LoginServlet
 *
 */
public class AjouterElementServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#HttpServlet()
     */

    public AjouterElementServlet() {
        super();
    }

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /* (non-Java-doc)
     * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    int i;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            //String StockName= request.getAttribute("selectid").toString();
           // System.out.print("stockage name="+StockName);
            HttpSession session = request.getSession(true);
            System.out.print("vao day ko?");
            ElementBean f=new ElementBean();
            f.setSelectid(request.getAttribute("selectid").toString());
            f.setCatalog(request.getAttribute("catalog").toString());
             f.setNomobjet(request.getAttribute("nomobjet").toString());
              f.setModelobjet(request.getAttribute("modelobjet").toString());
              f.setStatus(request.getAttribute("status").toString());
              f.setSerie(request.getAttribute("serie").toString());
              f.setVendeur(request.getAttribute("vendeur").toString());
              f.setAdress(request.getAttribute("adress").toString());
              f.setVille(request.getAttribute("ville").toString());
              f.setZip(request.getAttribute("zip").toString());
              f.setPays(request.getAttribute("pays").toString());
              f.setJour(request.getAttribute("jour").toString());
               f.setMois(request.getAttribute("mois").toString());
                f.setAn(request.getAttribute("an").toString());
                  f.setPhoto1(request.getAttribute("photo1").toString());
                f.setPhoto2(request.getAttribute("photo2").toString());
             System.out.print("day la selectid " + f.getSelectid());
           String user1 = session.getAttribute("user").toString();
           String UserRoleID = session.getAttribute("UserRoleID").toString();
            System.out.println("day la user " + session.getAttribute("user"));
            System.out.println("voi roleID " + UserRoleID);
            // System.out.print("day la:"+f.getNomobjet());
            MySQLDBConnector connec = new MySQLDBConnector();
            int UserID = connec.getUserID(user1);
            session.setAttribute("UserID", UserID);
            System.out.print("UserID=" + UserID);
            int rel = connec.InserObjet(session, f, f.getSelectid());
            System.out.print("hoi xem rel la bao nhieu"+ rel);
            boolean isMultipart = ServletFileUpload.isMultipartContent(request);
// Create a factory for disk-based file items


            System.out.print(rel);
            if (rel == 0) {

                request.setAttribute("user", user1);

                String url = "/retry.jsp?username=" + user1;
                RequestDispatcher disp;
                disp = request.getRequestDispatcher(url);
                disp.forward(request, response);

            }
            if (rel == 1) {
                 System.out.print(request.getAttribute("photo1"));
                 System.out.println("CM "+request.getAttribute("user"));
                request.setAttribute("user", user1);
                String url = "PHAN/AjouterElementSuccess.jsp?username=" + user1;
                RequestDispatcher disp;
                disp = request.getRequestDispatcher(url);
                disp.forward(request, response);

            }
            if (rel == 2) {
                 request.setAttribute("user", user1);
                String url = "PHAN/ObjetPlus10.jsp?username=" + user1;
                RequestDispatcher disp;
                disp = request.getRequestDispatcher(url);
                disp.forward(request, response);
            }
            if (rel == 3) {
                request.setAttribute("user", user1);
                String url = "PHAN/ValidePaiement.jsp";
                RequestDispatcher disp;
                disp = request.getRequestDispatcher(url);
              // disp.forward(request, response);
            }


        } catch (Exception e) {
            System.out.print("loi la "+e.getMessage());
        }
    }
}